/*
 * Class: CustomerRefundAdapter
 * Created by: Mary Joy Pascual
 * Date Created: 06/25/2014
 * Company: Netsuite
*/
package com.netsuite.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.netsuite.constants.Constants;
import com.netsuite.dao.AccountDAO;
import com.netsuite.dao.CurrencyDAO;
import com.netsuite.dao.CustomerDAO;
import com.netsuite.dao.CustomerDepositDAO;
import com.netsuite.dao.DepositApplicationDAO;
import com.netsuite.dao.PaymentMethodDAO;
import com.netsuite.helper.ErrorHelper;
import com.netsuite.logger.LoggerImp;
import com.netsuite.pojo.CustomerRefundPOJO;
import com.netsuite.webservices.lists.accounting_2014_1.Currency;
import com.netsuite.webservices.platform.core_2014_1.RecordRef;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerRefund;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerRefundApply;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerRefundApplyList;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerRefundDeposit;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerRefundDepositList;

public class CustomerRefundAdapter {

	private static String CLASS_NAME = CustomerRefundAdapter.class.getName();
	
	public static CustomerRefund setCustomerRefund(CustomerRefundPOJO cusRefPojo) throws ErrorHelper, ParseException {
		
		LoggerImp.startLog(CLASS_NAME, "setCustomerRefund() - CustomerRefundPOJO to CustomerRefund");
		
		CustomerRefund cusRefund = new CustomerRefund();
		cusRefund.setExternalId(cusRefPojo.getExternalId());
		cusRefund.setTranId(cusRefPojo.getTranId());
		cusRefund.setExchangeRate(Double.parseDouble(cusRefPojo.getExchangeRate()));
		cusRefund.setMemo(cusRefPojo.getMemo());
		
		DateFormat df = new SimpleDateFormat(Constants.DATE_FORMAT);
		Calendar tranDate  = Calendar.getInstance();
		tranDate.setTime(df.parse(cusRefPojo.getDate()));
		cusRefund.setTranDate(tranDate);
		
		RecordRef customer = new RecordRef();
		CustomerDAO customerDAO = new CustomerDAO();
		customer.setInternalId(customerDAO.getCustomerInternalId(cusRefPojo.getCustomerName()));
		cusRefund.setCustomer(customer);
		
		RecordRef account = new RecordRef();
		AccountDAO acctDAO = new AccountDAO();
		account.setInternalId(acctDAO.getAccountInternalId(cusRefPojo.getAccountName()));
		cusRefund.setAccount(account);
		
		RecordRef refundMethod = new RecordRef();
		PaymentMethodDAO payment = new PaymentMethodDAO();
		refundMethod.setInternalId(payment.getPaymentMethodInternalId(cusRefPojo.getRefMethod()));
		cusRefund.setPaymentMethod(refundMethod);
		
		RecordRef currency = new RecordRef();
		currency.setInternalId(getCurrency(cusRefPojo.getCurrency()));
		cusRefund.setCurrency(currency);
	
		if(cusRefPojo.getCreditRef().length() != 0 && cusRefPojo.getCreditPayment().length() != 0){
			DepositApplicationDAO depApp = new DepositApplicationDAO();
			String depId = depApp.getDepositInternalId(cusRefPojo.getCreditRef());
			CustomerRefundApplyList cusApplyList = new CustomerRefundApplyList();
			cusApplyList.setApply(new CustomerRefundApply[1]);
			cusApplyList.setApply(0, new CustomerRefundApply());
			cusApplyList.getApply(0).setDoc(Long.parseLong(depId));
			cusApplyList.getApply(0).setAmount(Double.parseDouble(cusRefPojo.getCreditPayment()));
			cusApplyList.getApply(0).setApply(true);
			cusRefund.setApplyList(cusApplyList);
		}
		
		if(cusRefPojo.getDebitRef().length() != 0 && cusRefPojo.getDebitPayment().length() != 0){
			CustomerDepositDAO cus = new CustomerDepositDAO();
			String depId = cus.getDepositInternalId(cusRefPojo.getDebitRef());
			CustomerRefundDepositList cusDepList = new CustomerRefundDepositList( );
			cusDepList.setCustomerRefundDeposit(new CustomerRefundDeposit[1]);
			cusDepList.setCustomerRefundDeposit(0, new CustomerRefundDeposit());
			cusDepList.getCustomerRefundDeposit(0).setDoc(Long.parseLong(depId));
			cusDepList.getCustomerRefundDeposit(0).setAmount(Double.parseDouble(cusRefPojo.getDebitPayment()));
			cusDepList.getCustomerRefundDeposit(0).setApply(true);
			cusRefund.setDepositList(cusDepList);
		}
		

		LoggerImp.endLog(CLASS_NAME, "setCustomerRefund() - CustomerRefundPOJO to CustomerRefund");		
		
		return cusRefund;
		
	}

	private static String getCurrency(String currency) throws ErrorHelper {
		CurrencyDAO currDAO = new CurrencyDAO();
		List<Currency> currList = currDAO.getAllCurrencies();
		for (Currency curr : currList){
			if(curr.getSymbol().equalsIgnoreCase(currency)){
				return curr.getInternalId();
			}
		}
		return "";
	}
}
